package com.lp.RSTX.controller;


import com.lp.RSTX.utils.UploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Api("压缩文件")
@Controller
@Component
@RequestMapping(value = "/user")
public class Unz {


    private String paths = "/data/录屏记录";

    @ResponseBody
    @PostMapping(value = "/upload")
    @ApiOperation(value = "压缩文件")
    public String unzip(@ApiParam(value = "压缩zip文件") MultipartFile jarFile, HttpServletRequest request, String name) {

        String path = "/data/录屏记录/" + name + "/";
        log.info("path:" + path);
        File file = new File(path);

        if (!file.exists()) {
            file.mkdir();
            log.info(file + "文件夹创建成功");
        }
        // 解压
        try {
            UploadUtil.resolveCompressUploadFile(request, jarFile, path);
            log.info("解压成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "200";
    }

    @Scheduled(cron = "0 30 0 * * ?")//定时每天0.30删除文件
    public void deletes() {
        deleteFiles(paths);
    }

    public void deleteFiles(String path) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        File fileDir = new File(path);

        File[] fileList = fileDir.listFiles();
        for (int i = 0; i < fileList.length; i++) {
            if (fileList[i].isDirectory()) {

                log.info(fileList[i] + "是文件夹\n");
                String path1 = fileList[i].getPath();
                deleteFiles(path1);
            } else {
                log.info(fileList[i].getPath());
                long time = new Date().getTime();//获取现在的时间
                long lastModified = fileList[i].lastModified();
                //String today = sdf.format(new Date());

                SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
                String today = sdf2.format(new Date());


                String s = sdf.format(new Date(lastModified));
                log.info("目标文件的最后修改时间为：" + s);

                Long aLong = time - lastModified;//当前时间减去文件的时间
                int a = (int) (aLong / (1000 * 60 * 60 * 24));//得到距离现在的天数

                log.info("目标文件距离" + today + "  " + a + "天");

                if (a > 30) {//大于30天

                    try {
                        Files.delete(Paths.get(fileList[i].toString()));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    log.info("删除成功\n");
                } else {
                    log.info("删除失败\n");
                }
            }

        }
    }
}